<template>
  <el-form 
    ref="loginForm"
    :model="form" 
    :rules="rules" 
    label-width="auto" 
    style="max-width: 600px">
    <el-form-item label="账号" prop="userName">
      <el-input v-model="form.userName" />
    </el-form-item>
    <el-form-item label="密码" prop="passWord">
      <el-input type="password" v-model="form.passWord" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import md5 from 'js-md5'

export default {
  name: 'login',
  data(){
    return {
      rules:{
        userName: [
          { required: true, message: '账号不能为空', trigger: 'blur' }
        ],
        passWord: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是6-15位非空字符', trigger: 'blur' }
        ]
      },
      form:{
        userName: '',
        passWord:''
      }
    }
  },
  methods:{
    async onSubmit(){
      this.$refs.loginForm.validate(async valid => {
        if(valid){
          let res = await this.$post(`/login`,{
            userName:this.form.userName,
            passWord:md5(this.form.passWord)
          })
          if(res.token){
            sessionStorage.setItem('token',res.token)
            this.$router.push({ path: '/index' })
          }
        }
      })
      
    }
  },
  mounted(){
    let id = this.$route.params.id
    console.log(id)
  }
}
</script>
